<?php


class gerar_editar {
    protected $tabela;
    protected $classe;
    protected $c;
    protected $r;
    protected $campos = array();
    
    
    public function __construct() {
        $this->c = model_conn::get_conn();
        $this->r = $this->c->getDatabaseInfo();
        
    }

    public function setTabela($tabela) {
        $this->tabela = $tabela;
    }

    public function setNomeClasse($prefixo, $sufixo) {
        $this->classe = $prefixo . $sufixo;
    }

    public function adicionarCampo($campo) {
        array_push($this->campos, $campo);
    }
    public function getResultado() {
        $r = '';
        
        $r.= "<?

class $this->classe extends visao {
    protected \$msg_erro_post = NULL;
    public function render() {
                
                
                ?>
        <h3>Editar $this->tabela</h3>
        <?
        if (\$this->recebeu_post()) {
            if (\$this->ocorreu_erro_post()) {
                echo '<div class=\"erro_preenchimento\">' . \$this->msg_erro_post . '</div>';
                \$this->form();
            } else {
                \$this->salva_$this->tabela();
            }
        } else {
            \$this->form();
        }
    }
                
                
                protected function form() {

        try {
            $$this->tabela = EASYNC__$this->tabela::getByPK(util::GET('id'));
        } catch (MARCEL_ORM_ObjectNotFoundException \$ex) {
            echo \"$this->tabela não existe.\";
            exit();
        }
                
                ?>
        <form action=\"?acao=$this->tabela.editar&id=<? echo \$_GET['id']; ?>\" method=\"post\">
            <input type=\"hidden\" name=\"post_recebido\" value=\"1\" />
            <table border=\"0\" cellpadding=\"4\" width=\"100%\">
               
                
                ";
        
        foreach ($this->r as $tables) {
            foreach ($tables as $table => $columns) {
                if ($table == $this->tabela) {
                    foreach ($columns as $column => $column_info) {
                        if ($column_info[0] == 0) {
                            $r.= $this->campo($column);
                        }
                    }
                }
            }
        }
        
        
        $r.= "</table>
            <br>
            <input type=\"submit\" value=\"Salvar\" />
        </form>
        <?
    }
    


    protected function recebeu_post() {

        if (!isset(\$_POST['post_recebido'])) {
            return false;
        }
        return true;
    }

    protected function ocorreu_erro_post() {
        return false;
    }

    protected function salva_$this->tabela() {

        $$this->tabela = EASYNC__$this->tabela::getByPK(util::GET('id'));";
        
        
        foreach ($this->r as $tables) {
            foreach ($tables as $table => $columns) {
                if ($table == $this->tabela) {
                    foreach ($columns as $column => $column_info) {
                        if ($column_info[0] == 0) {
                            $r.= "\n$$this->tabela->set" . ucfirst($column) . "(util::POST('$column'));";
                        }
                    }
                }
            }
        }
        
        
    $r.= "
        $$this->tabela->save();
        ?>
        Dados de $this->tabela alterados com sucesso.
        <?
    }

}
?>";
        return $r;
    }
    
    
    
    
    protected function campo($campo) {
        $r = '<tr>
                    <td nowrap="nowrap" bgcolor="#666666" valign="top"><span class="style3">'.$campo.':</span></td>
                    <td valign="top" bgcolor="#EBEBEB"><input name="'.$campo.'" type="text" value="<?
                if ($this->recebeu_post()) {
                    echo util::POST(\''.$campo.'\');
                } else {
                    echo $'.$this->tabela.'->get'.  ucfirst($campo).'();
                }
        ?>" /></td>
                </tr>';
        
        return $r;
    }
    
    
}

?>
